package model;

import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import view.MediaFormatTypeEnum;


public class MediaType{
	// Members held by class
	public static final ArrayList<MediaType> types;
	public static final MediaType defaultType;
	
	// Members held by instances
	public ArrayList<MediaProperty> properties = new ArrayList<MediaProperty>();
	public ArrayList<MediaType> baseTypes = new ArrayList<MediaType>();
	public String name;
	public String displayName;
	
	// Static methods
	static {
		types = new ArrayList<MediaType>();

		defaultType = new MediaType("","");
		
		// properties that all types will inherit
		defaultType.properties.add(new MediaProperty("File Name","name", true, true, false, true, false, MediaFormatTypeEnum.DOWNLOAD_LINK)); //not editable atm
		defaultType.properties.add(new MediaProperty(null,"key", false, false, false, false, false, MediaFormatTypeEnum.DEFAULT) ); // not searchable
		defaultType.properties.add(new MediaProperty("File Size","size", true, false, false, true, false, MediaFormatTypeEnum.FILE_SIZE) );
		
		MediaType image = new MediaType("image", "Pictures");
		image.properties.add(new MediaProperty("Caption","title", true, true, true, true, true, MediaFormatTypeEnum.DEFAULT) );
		image.properties.add(new MediaProperty("Description","desc", true, true, true, true, true, MediaFormatTypeEnum.DEFAULT) ); 
		image.baseTypes.add(defaultType); // make the default type an 'ancestor'
		types.add(image);
		
		MediaType video = new MediaType("video", "Videos");
		video.properties.add(new MediaProperty("Title","title", true, true, true, true, true, MediaFormatTypeEnum.DEFAULT) );
		video.properties.add(new MediaProperty("Director","artist", true, true, true, true, false, MediaFormatTypeEnum.DEFAULT) );
		video.properties.add(new MediaProperty("Actors","actor", true, true, true, true, false, MediaFormatTypeEnum.DEFAULT) );
		video.properties.add(new MediaProperty("Genre","genre", true, true, true, true, false, MediaFormatTypeEnum.DEFAULT) );
		video.properties.add(new MediaProperty("Synopsis","desc", true, true, true, true, true, MediaFormatTypeEnum.DEFAULT) ); 
		video.baseTypes.add(defaultType); // make the default type an 'ancestor'
		types.add(video);
		
		
		MediaType audio = new MediaType("audio", "Audio");
		audio.properties.add(new MediaProperty("Title","title", true, true, true, true, true, MediaFormatTypeEnum.DEFAULT) );
		audio.properties.add(new MediaProperty("Artist","artist", true, true, true, true, true, MediaFormatTypeEnum.DEFAULT) );
		audio.properties.add(new MediaProperty("Album","album", true, true, true, true, true, MediaFormatTypeEnum.DEFAULT) );
		audio.properties.add(new MediaProperty("Genre","genre", true, true, true, true, true, MediaFormatTypeEnum.DEFAULT) );
		audio.baseTypes.add(defaultType); // make the default type an 'ancestor'
		types.add(audio);
		
	}
	
	public static MediaType getMediaType(String name){
		if(name == null){ return defaultType; }
		Iterator<MediaType> iter = types.iterator();
		while(iter.hasNext()){
			MediaType type = iter.next();
			if(type.name.equals(name)){
				return type;
			}
		}
		return defaultType;
	}
	
	// Instance methods
	public MediaType(String name, String displayName){
		this.name = name;
		this.displayName = displayName;
	}
	
	public TreeSet<MediaProperty> getAllMediaProperties(){
		TreeSet<MediaProperty> props = new TreeSet<MediaProperty>();
		props.addAll(properties);
		Iterator<MediaType> iter = baseTypes.iterator();
		while(iter.hasNext()){
			MediaType type = iter.next();
			props.addAll(type.getAllMediaProperties());
		}
		return props;
	}
	
	public MediaProperty getMediaProperty(String name){
		if(name == null){ return null; }
		
		// Search for properties from the base types as well
		for (MediaType base : baseTypes) {
			if (base.getMediaProperty(name) != null) {
				return base.getMediaProperty(name);
			}
		}
		Iterator<MediaProperty> iter = properties.iterator();
		while(iter.hasNext()){
			MediaProperty prop = (MediaProperty) iter.next();
			if(prop.name.equals(name)){
				return prop;
			}
		}
		return null;
	}
}
